package com.svs.web.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.svs.common.annotation.Log;
import com.svs.common.base.AjaxResult;
import com.svs.common.enums.BusinessType;
import com.svs.framework.shiro.util.ShiroUtils;
import com.svs.framework.web.BaseController;
import com.svs.system.cache.SysAreaCache;
import com.svs.system.domain.Area;
import com.svs.system.domain.AreaNode;
import com.svs.system.service.AreaService;

/**
 * 行政区划 信息操作处理
 * 
 * @author dujianqiao
 * @date 2019-01-02
 */
@Controller
@RequestMapping("/module/area")
public class AreaController extends BaseController {

	private String prefix = "system/area";

	@Autowired
	private AreaService areaService;

	@RequiresPermissions("module:area:view")
	@GetMapping()
	public String area() {
		return prefix + "/areaList";
	}

	/**
	 * 查询行政区划列表
	 */
	@RequiresPermissions("module:area:list")
	@GetMapping("/list")
	@ResponseBody
	public List<AreaNode> list(Area area) {
		String code = area.getAreaCode() ;
		if(StringUtils.isEmpty(code)) {
			code = "0" ;
		}
		List<AreaNode> list = SysAreaCache.getCache().getChrildrens(code);
		List<AreaNode> results = new ArrayList<AreaNode>() ;
		for(AreaNode f : list) {
			List<AreaNode> chList = SysAreaCache.getCache().getChrildrens(f.getAreaCode());
			results.add(f) ;
			for(AreaNode fc : chList) {
				results.add(fc) ;
				List<AreaNode> fchList = SysAreaCache.getCache().getChrildrens(fc.getAreaCode());
				results.addAll(fchList) ;
			}
			
		}
		return results;
	}


	/**
	 * 查询行政区划列表
	 */
	@RequiresPermissions("module:area:list")
	@GetMapping("/chrildrens")
	@ResponseBody
	public List<AreaNode> chrildrens(Area area) {
		return SysAreaCache.getCache().getChrildrens(area.getAreaCode()) ;
	}

	/**
	 * 新增行政区划
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") String parentId, ModelMap mmap) {
		AreaNode area = SysAreaCache.getCache().getAreaById(parentId) ;
		if(null == area) {
			area = new AreaNode() ;
			area.setParentCode("0");
			area.setAreaCode("0");
			area.setTreeNames("");
		}
		mmap.addAttribute("parentArea", area) ;
		return prefix + "/addArea";
	}

	/**
	 * 新增保存行政区划
	 */
	@RequiresPermissions("module:area:add")
	@Log(title = "行政区划", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Area area) {
		area.setCreateBy(ShiroUtils.getLoginName());
		area.setCreateDate(new Date());
		int result = areaService.save(area) ;
		SysAreaCache.getCache().init();
		return toAjax(result);
	}

	/**
	 * 修改行政区划
	 */
	@GetMapping("/edit/{areaCode}")
	public String edit(@PathVariable("areaCode") String areaCode, ModelMap mmap) {
		Area area = areaService.selectByPrimaryKey(areaCode);
		mmap.put("area", area);
		return prefix + "/editArea";
	}

	/**
	 * 修改保存行政区划
	 */
	@RequiresPermissions("module:area:edit")
	@Log(title = "行政区划", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Area area) {
		area.setCreateBy(ShiroUtils.getLoginName());
		area.setCreateDate(new Date());
		int result = areaService.update(area) ;
		SysAreaCache.getCache().init();
		return toAjax(result);
	}

	/**
	 * 删除行政区划
	 */
	@RequiresPermissions("module:area:remove")
	@Log(title = "行政区划", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(areaService.deleteByPrimaryKey(ids));
	}

}
